package com.springboot.demo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.springboot.demo.entity.*;
import com.springboot.demo.exception.BusinessException;
import com.springboot.demo.mapper.AlarmMapper;
import com.springboot.demo.mapper.SceneMapper;
import com.springboot.demo.mapper.UserMapper;
import com.springboot.demo.service.AlarmService;
import com.springboot.demo.service.UserService;
import com.springboot.demo.utils.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author wubc683
 * @desc
 * @date 2021-12-23 16:07
 */
@Service
public class AlarmServiceImpl implements AlarmService {

    @Autowired
    AlarmMapper alarmMapper;
    @Autowired
    PageUtils pageUtils;
    @Autowired
    SceneMapper sceneMapper;
    @Autowired
    UserService userService;
    @Autowired
    UserMapper userMapper;

    private String[] dataFieldsSorted = {"varName", "deviceName", "sceneName", "dataType", "value", "productTime"};
    private String[] alarmFieldsSorted = {"varName", "deviceName", "sceneName", "dataType", "value", "productTime",
            "alarmType", "alarmText"};

    @Override
    public PageResult getHistoryDataInfo(Long userId, Long sceneId, Date startDate, Date endDate, String sort,
                                         String search, PageRequest pageRequest) {
        // 校验起始日期和结束日期
        if (startDate != null && endDate != null) {
            if (startDate.compareTo(endDate) > 0) {
                throw new BusinessException(ResponseResult.START_DATE_LARGER_THAN_END_DATE);
            }
        }
        // 校验sort
        CommonUtils.checkSortValidity(sort, Arrays.asList(dataFieldsSorted));
        // 校验场景id
        if (sceneId != null) {
            Scene sceneById = sceneMapper.getSceneById(sceneId);
            if (sceneById == null || !sceneById.getOwnerId().equals(userId)) {
                throw new BusinessException(ResponseResult.USER_DO_NOT_HAVE_THIS_SCENE);
            }
        }
        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<HistoryData> historyDataInfo = alarmMapper.getHistoryDataInfo(userId, sceneId, startDate, endDate, sort,
                search);
        // 去除小数尾部多余的0
        for(HistoryData h:historyDataInfo){
            h.setValue(CommonUtils.removeTailZero(h.getValue()));
        }
        return pageUtils.getPageResult(pageRequest, new PageInfo<>(historyDataInfo));
    }

    @Override
    public PageResult getHistoryAlarmInfo(Long userId, Long sceneId, Date startDate, Date endDate, String sort,
                                          String search, PageRequest pageRequest) {
        // 校验起始日期和结束日期
        if (startDate != null && endDate != null) {
            if (startDate.compareTo(endDate) > 0) {
                throw new BusinessException(ResponseResult.START_DATE_LARGER_THAN_END_DATE);
            }
        }
        // 校验sort
        CommonUtils.checkSortValidity(sort, Arrays.asList(alarmFieldsSorted));
        // 校验场景id
        if (sceneId != null) {
            Scene sceneById = sceneMapper.getSceneById(sceneId);
            if (sceneById == null || !sceneById.getOwnerId().equals(userId)) {
                throw new BusinessException(ResponseResult.USER_DO_NOT_HAVE_THIS_SCENE);
            }
        }

        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        List<HistoryAlarm> historyAlarmInfo = alarmMapper.getHistoryAlarmInfo(userId, sceneId, startDate, endDate,
                sort, search);
        // 去除小数尾部多余的0
        for(HistoryAlarm h:historyAlarmInfo){
            h.setValue(CommonUtils.removeTailZero(h.getValue()));
        }
        return pageUtils.getPageResult(pageRequest, new PageInfo<>(historyAlarmInfo));
    }

    @Override
    public Integer getHistoryAlarmCount(Long userId) {
        Integer historyRecordCount = alarmMapper.getHistoryRecordCount(userId);
        return historyRecordCount;
    }

    @Override
    public void setHistoryAlarmCount(Long userId, Integer count) {
        // 检查user是否是管理员
        User userInfoById = userService.getUserInfoById(userId);
        if(userInfoById == null || !userInfoById.getRid().equals(Constant.ADMIN)){
            throw new BusinessException(ResponseResult.USER_NOT_EXIST_OR_NOT_ADMIN);
        }
        userMapper.setUserAlamrRecordCount(userId,count);
    }

    @Override
    public void deleteHistoryAlarm(Long userId, Date startDate, Date endDate) {
        if(startDate != null && endDate != null && startDate.compareTo(endDate) > 0){
            throw new BusinessException(ResponseResult.START_DATE_LARGER_THAN_END_DATE);
        }
        alarmMapper.deleteHistoryAlarm(userId,startDate,endDate);
    }

    @Override
    public void deleteHistoryData(Long userId, Date startDate, Date endDate) {
        if(startDate != null && endDate != null && startDate.compareTo(endDate) > 0){
            throw new BusinessException(ResponseResult.START_DATE_LARGER_THAN_END_DATE);
        }
        alarmMapper.deleteHistoryData(userId,startDate,endDate);
    }
}
